package com.wpam.scrabble.mobile.ui;

import com.wpam.scrabble.mobile.GameLogic;
import com.wpam.scrabble.mobile.GameResources;
import com.wpam.scrabble.mobile.R;
import com.wpam.scrabble.mobile.communication.ToastRunnable;

import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;

public class LetterChangePanel extends Drawable {

	public static final int TILE_SIZE = 36;
	
	private RectF letterChangePanel_;
	private Paint panelPaint_;
	private LetterChangeSquare[] squares_;
	private int tileSize_;
	private ButtonDrawable buttonOk_;
	private ButtonDrawable buttonCancel_;
	
	public LetterChangePanel() {
		
		Resources resources = GameResources.getInstance().getResources();
		int width = resources.getDisplayMetrics().widthPixels;
		int height = resources.getDisplayMetrics().heightPixels;
		this.tileSize_ = TILE_SIZE;
		this.buttonOk_ = new ButtonDrawable((int) (0.11 * width), (int) (0.7 * height), (int) (0.35 * width), (int) (0.1 * height), resources.getString(R.string.change_letters));
		this.buttonCancel_ = new ButtonDrawable((int)(0.54 * width), (int)(0.7 * height), (int) (0.35 * width), (int) (0.1 * height), resources.getString(R.string.cancel));
		this.letterChangePanel_ = new RectF((float)(0.1 * width), (float)(0.1 * height), (float)(0.9 * width), (float)(0.9 * height));
		this.panelPaint_= new Paint();
		this.panelPaint_.setColor(Color.rgb(205, 233, 236));
		this.panelPaint_.setAlpha(200);
		this.squares_ = new LetterChangeSquare[7];
		
		int maxInRow = (int) ((0.7 * width) / (1.2 * tileSize_));
		int rowsLeft = (int) Math.ceil(7.0 / maxInRow);
		int inLastRowLeft = maxInRow * (1 - rowsLeft) + 7;
		int spacer = (int) ((0.7 * width - maxInRow * tileSize_) / (maxInRow - 1));
		int lastRowSpacer = (int) ((0.7 * width - inLastRowLeft * tileSize_) / (inLastRowLeft + 1));
		int leftLimit = (int) (width * 0.15);
		int posX = leftLimit;
		int posY = (int) (height * 0.2);
		int mod = 0;
		for (int i = 0; i < 7; ++i) {
			if (rowsLeft > 1) {
				if (maxInRow > i - mod) {
					this.squares_[i] = new LetterChangeSquare(posX, posY, tileSize_);
					posX += tileSize_ + spacer;
				} else {
					--rowsLeft;
					mod = i;
					posX = leftLimit;
					posY += 1.2 * tileSize_;
					--i;
				}
			} else {
				posX += lastRowSpacer;
				this.squares_[i] = new LetterChangeSquare(posX, posY, tileSize_);
				posX += tileSize_;
			}
		}
	}

	public void resize() {
		
		int width = GameResources.getInstance().getResources().getDisplayMetrics().widthPixels;
		int height = GameResources.getInstance().getResources().getDisplayMetrics().heightPixels;
		this.tileSize_ = TILE_SIZE;
		this.buttonOk_.onResize((int) (0.11 * width), (int) (0.7 * height), (int) (0.35 * width), (int) (0.1 * height));
		this.buttonCancel_.onResize((int)(0.54 * width), (int)(0.7 * height), (int) (0.35 * width), (int) (0.1 * height));
		this.letterChangePanel_.set((float)(0.1 * width), (float)(0.1 * height), (float)(0.9 * width), (float)(0.9 * height));
		
		int maxInRow = (int) ((0.7 * width) / (1.2 * tileSize_));
		int rowsLeft = (int) Math.ceil(7.0 / maxInRow);
		int inLastRowLeft = maxInRow * (1 - rowsLeft) + 7;
		int spacer = (int) ((0.7 * width - maxInRow * tileSize_) / (maxInRow - 1));
		int lastRowSpacer = (int) ((0.7 * width - inLastRowLeft * tileSize_) / (inLastRowLeft + 1));
		int leftLimit = (int) (width * 0.15);
		int posX = leftLimit;
		int posY = (int) (height * 0.2);
		int mod = 0;
		for (int i = 0; i < 7; ++i) {
			if (rowsLeft > 1) {
				if (maxInRow > i - mod) {
					this.squares_[i].onResize(posX, posY, tileSize_);
					posX += tileSize_ + spacer;
				} else {
					--rowsLeft;
					mod = i;
					posX = leftLimit;
					posY += 1.2 * tileSize_;
					--i;
				}
			} else {
				posX += lastRowSpacer;
				this.squares_[i].onResize(posX, posY, tileSize_);
				posX += tileSize_;
			}
		}
	}
	
	public void setLetters(LettersStand letterStand) {
		
		LettersStandSquare[] letterStandSquares = letterStand.getLetters();
		for (int i = 0; i < squares_.length; ++i)
			squares_[i].setLetter(letterStandSquares[i].getLetter());
	}

	public boolean handleClick(int x, int y) {

		GameBoard gameBoard = GameLogic.getInstance().getGameBoard();
		if (buttonCancel_.amIHit(x, y)) {
			gameBoard.showLetterChangePanel(false);
			return true;
		}
		if (buttonOk_.amIHit(x, y)) {
			StringBuilder letters = new StringBuilder();
			for (LetterChangeSquare square : squares_) {
				if (square.isSelected())
					letters.append(square.getLetter());
			}
			if (letters.length() > 0) {
				GameLogic.getInstance().changeLetters(letters.toString());
				GameLogic.getInstance().markMove(null);
				gameBoard.showLetterChangePanel(false);
			} else
				gameBoard.getHandler().post(new ToastRunnable(GameResources.getInstance().getResources().getString(R.string.select_letters_first), true));
			
			return true;
		}
		for (LetterChangeSquare square : squares_) {
			if(square.amIHit(x, y)) {
				square.toggle();
				gameBoard.invalidate();
				return true;
			}
		}
		return false;
	}
	
	@Override
	public void draw(Canvas canvas) {
		
		canvas.drawRoundRect(letterChangePanel_, tileSize_, tileSize_, panelPaint_);
		for (LetterChangeSquare square : squares_)
			square.draw(canvas);
		buttonOk_.draw(canvas);
		buttonCancel_.draw(canvas);
	}

	@Override
	public int getOpacity() {
		return 0;
	}

	@Override
	public void setAlpha(int alpha) {
		
		for (LetterChangeSquare square : squares_)
			square.setAlpha(alpha);
		buttonOk_.setAlpha(alpha);
		buttonCancel_.setAlpha(alpha);
	}

	@Override
	public void setColorFilter(ColorFilter cf) {

		for (LetterChangeSquare square : squares_)
			square.setColorFilter(cf);
		buttonOk_.setColorFilter(cf);
		buttonCancel_.setColorFilter(cf);
	}

}
